﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ATA.Business;
using ATA.Entities;
using Infragistics.Win.UltraWinDataSource;
using ATA.Data.MisaDAL;
using Infragistics.Win.UltraWinEditors;
using System.Configuration;

namespace ATA.Winforms.Report
{
    public partial class rptMain : Form
    {
        public rptMain()
        {
            InitializeComponent();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnCreateReport_Click(object sender, EventArgs e)
        {
            int iIndex = 0;
            //switch (lstRpt.SelectedIndex.ToString())
            //{
            //    case "0":
            //        iIndex = 0;
            //        break;
            //    case "1":
            //        iIndex = 1;
            //        break;
            //    case "2":
            //        iIndex = 2;
            //        break;
            //    case "3":
            //        iIndex = 3;
            //        break;
            //    case "4":
            //        iIndex = 4;
            //        break;
            //    case "5":
            //        iIndex = 5;
            //        break;
            //    case "6":
            //        iIndex = 6;
            //        break;
            //    case "7":
            //        iIndex = 7;
            //        break;
            //    case "8":
            //        iIndex = 8;
            //        break;
            //}
            //CreateReport(iIndex);
            CreateReport(lstRpt.SelectedIndex);
        }
        /// <summary>
        /// Tạo report
        /// </summary>
        /// <param name="iIndex"></param>
        private void CreateReport(int iIndex)
        {
            try
            {
                //frmRptView frm = new frmRptView(iIndex);
                string v_strCustomerID = string.Empty;
                string v_strBranchID = string.Empty;
                int v_iCustomerLevel = 1;
                int v_iMonth;
                int v_iYear;
                int v_iBrandID = 0;
                int v_iChuongTrinhDuThuongID = 0;
                string v_strChuongTrinhChietKhau = string.Empty;
                int v_iLoaiMayPhaMau = 0;

                bool v_bValidated = true;

                if (grdCustomers.ActiveRow.Index > -1)
                {
                    if (grdCustomers.ActiveRow.Cells["AccountingObjectID"].Value != null)
                    {
                        v_strCustomerID = grdCustomers.ActiveRow.Cells["AccountingObjectID"].Value.ToString();
                        v_iCustomerLevel = Convert.ToInt16(grdCustomers.ActiveRow.Cells["Level"].Value.ToString());
                    }
                    else
                    {
                        //v_bValidated = false;
                        //MessageBox.Show(this, "Chưa chọn khách hàng!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                else
                {
                    //v_bValidated = false;
                    //MessageBox.Show(this, "Chưa chọn khách hàng!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }

                v_iMonth = dtmStartDate.DateTime.Month;
                v_iYear = dtmStartDate.DateTime.Year;

                if (cboTrungTam.SelectedItem != null)
                {
                    v_strBranchID= cboTrungTam.SelectedItem.DataValue.ToString();
                }
                else
                {
                    v_bValidated = false;
                    MessageBox.Show(this, "Chưa chọn trung tâm!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }

                if (cboBrandNames.SelectedItem != null)
                {
                    v_iBrandID = Convert.ToInt16(cboBrandNames.SelectedItem.DataValue);
                }

                if (cboChuongtrinhduthuong.SelectedItem != null)
                {
                    v_iChuongTrinhDuThuongID = Convert.ToInt16(cboChuongtrinhduthuong.SelectedItem.DataValue);
                }

                if (cboChuongtrinhChietkhau.SelectedItem != null)
                {
                    v_strChuongTrinhChietKhau = cboChuongtrinhChietkhau.SelectedItem.DataValue.ToString();
                }

                if (rdoLoaiMay.Value != null)
                {
                    v_iLoaiMayPhaMau = Convert.ToInt16(rdoLoaiMay.Value);
                }
                if (v_bValidated)
                {
                    frmRptView v_frmReport = new frmRptView(iIndex, v_strBranchID,
                                                                        v_strCustomerID, v_iCustomerLevel,
                                                                        v_iMonth, v_iYear, v_iBrandID,
                                                                        v_iChuongTrinhDuThuongID, v_strChuongTrinhChietKhau,
                                                                        v_iLoaiMayPhaMau);
                    v_frmReport.Show();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }

        }

        private void rptMain_Load(object sender, EventArgs e)
        {
            LoadTrungTamList();
            LoadCustomerList();
            LoadBrandList();
            LoadChuongTrinhList();
            LoadChuongTrinhDuThuong();
        }

        private void LoadBrandList()
        {
            //ThuongHieuService v_srvThuongHieu = new ThuongHieuService();
            //TList<ThuongHieu> v_lsThuongHieu = v_srvThuongHieu.Find("IsActive = TRUE");

            cboBrandNames.ValueMember = "BrandID";
            cboBrandNames.DisplayMember = "BrandName";
            cboBrandNames.DataSource = Globals.gv_lsThuongHieu;
            cboBrandNames.DataBind();
        }

        private void LoadChuongTrinhList()
        {
            ChuongTrinhService v_srvChuongTrinh = new ChuongTrinhService();
            TList<ChuongTrinh> v_lsChuongTrinh = v_srvChuongTrinh.Find("IsActive = TRUE");

            cboChuongtrinhduthuong.ValueMember = "ChuongTrinhID";
            cboChuongtrinhduthuong.DisplayMember = "TenChuongTrinh";
            cboChuongtrinhduthuong.DataSource = v_lsChuongTrinh;
            cboChuongtrinhduthuong.DataBind();
        }

        private void LoadChuongTrinhDuThuong()
        {
            ChietKhauService v_srvChietKhau = new ChietKhauService();
            TList<ATA.Entities.ChietKhau> v_lsChuongTrinh = v_srvChietKhau.GetAll();

            cboChuongtrinhChietkhau.ValueMember = "ChietKhauID";
            cboChuongtrinhChietkhau.DisplayMember = "Header";
            cboChuongtrinhChietkhau.DataSource = v_lsChuongTrinh;
            cboChuongtrinhChietkhau.DataBind();
        }

        private void LoadTrungTamList()
        {
            if (Globals.gv_bIsGlobalBranch)
            {
                ATA.Data.MisaDAL.DAL dal = new ATA.Data.MisaDAL.DAL(ConfigurationManager.ConnectionStrings["misaConnectionString"].ConnectionString, ConfigurationManager.ConnectionStrings["netTiersConnectionString"].ConnectionString);
                DataSet v_dsBranches = dal.GetAllActiveBranchs();
                cboTrungTam.DataSource = v_dsBranches;
                cboTrungTam.DataMember = v_dsBranches.Tables[0].TableName;
                cboTrungTam.DisplayMember = "BranchName";
                cboTrungTam.ValueMember = "BranchID";
                cboTrungTam.DataBind();
            }
            else
            {
                cboTrungTam.Items.Add(Globals.gv_strBranchID, Globals.gv_strBranchName);
            }
        }

        private void LoadCustomerList()
        {
            foreach (AccountingObject v_lsParentCusomers in Globals.gv_lsCustomers)
            {
                UltraDataRow row = dsCustomers.Rows.Add();
                row["AccountingObjectId"] = v_lsParentCusomers.AccountingObjectId.ToString();
                row["ParentAccountingObjectId"] = v_lsParentCusomers.ParentAccountingObjectId.ToString();
                row["AccountingObjectName"] = v_lsParentCusomers.AccountingObjectName;
                row["AccountingObjectCode"] = v_lsParentCusomers.AccountingObjectCode;
                row["Level"] = 1;
                foreach (AccountingObject v_lsChildCusomers in Globals.gv_lsCustomersNotInMisa)
                {
                    if (v_lsChildCusomers.ParentAccountingObjectId.Equals(v_lsParentCusomers.AccountingObjectId))
                    {
                        UltraDataRowsCollection childRows = row.GetChildRows("Band 1");
                        UltraDataRow childRow = childRows.Add();
                        childRow["ParentAccountingObjectId"] = v_lsChildCusomers.ParentAccountingObjectId.ToString();
                        childRow["AccountingObjectId"] = v_lsChildCusomers.AccountingObjectId.ToString();
                        childRow["AccountingObjectName"] = v_lsChildCusomers.AccountingObjectName;
                        childRow["AccountingObjectCode"] = v_lsChildCusomers.AccountingObjectCode;
                        childRow["Level"] = 2;
                    }
                }

            }

            grdCustomers.DataSource = dsCustomers;
            grdCustomers.DisplayLayout.Bands[0].Columns["AccountingObjectID"].Hidden = true;
            grdCustomers.DisplayLayout.Bands[0].Columns["ParentAccountingObjectID"].Hidden = true;
            grdCustomers.DisplayLayout.Bands[0].Columns["Level"].Hidden = true;
            grdCustomers.DisplayLayout.Bands[0].Columns["AccountingObjectCode"].Header.Caption = "Mã KH";
            grdCustomers.DisplayLayout.Bands[0].Columns["AccountingObjectCode"].Width = 120;
            grdCustomers.DisplayLayout.Bands[0].Columns["AccountingObjectName"].Header.Caption = "Tên KH";
            grdCustomers.DisplayLayout.Bands[0].Columns["AccountingObjectName"].Width = 250;
            grdCustomers.DisplayLayout.Bands[1].Columns["AccountingObjectID"].Hidden = true;
            grdCustomers.DisplayLayout.Bands[1].Columns["ParentAccountingObjectID"].Hidden = true;
            grdCustomers.DisplayLayout.Bands[1].Columns["Level"].Hidden = true;
            grdCustomers.DisplayLayout.Bands[1].Columns["AccountingObjectCode"].Header.Caption = "Mã KH";
            grdCustomers.DisplayLayout.Bands[1].Columns["AccountingObjectCode"].Width = 120;
            grdCustomers.DisplayLayout.Bands[1].Columns["AccountingObjectName"].Header.Caption = "Tên KH";
            grdCustomers.DisplayLayout.Bands[1].Columns["AccountingObjectName"].Width = 250;
            grdCustomers.Width = 520;
            grdCustomers.DataBind();

            DropDownEditorButton objKhachHang = new DropDownEditorButton();
            objKhachHang.Control = this.grdCustomers;
            txtCustomers.ButtonsRight.Add(objKhachHang);
        }

        private void txtCustomers_AfterEditorButtonCloseUp(object sender, EditorButtonEventArgs e)
        {
            if (grdCustomers.ActiveRow.Index > -1)
            {
                if (grdCustomers.ActiveRow.Cells["AccountingObjectID"].Value != null)
                {
                    txtCustomers.Text = grdCustomers.ActiveRow.Cells["AccountingObjectCode"].Value.ToString();
                    txtCustomerName.Text = grdCustomers.ActiveRow.Cells["AccountingObjectName"].Value.ToString();
                }
            }
        }

        private void grdCustomers_DoubleClickRow(object sender, Infragistics.Win.UltraWinGrid.DoubleClickRowEventArgs e)
        {
            txtCustomerName.Focus();
        }

        private void grdCustomers_DoubleClick(object sender, EventArgs e)
        {
            txtCustomerName.Focus();
        }
    }
}
